<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>函数参数详解 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter08/section04.html" />
    
    
    <link rel="prev" href="../chapter08/section02.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="8.3"
        data-chapter-title="函数参数详解"
        data-filepath="chapter08/section03.md"
        data-basepath=".."
        data-revision="Sun Feb 09 2020 13:02:11 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="../chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        什么是函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="../chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        函数的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="../chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        函数的参数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="../chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        函数的返回值
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="../chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        文档注释
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="../chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        函数练习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="../chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        函数调用函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08/index.html">
            
                
                    <a href="../chapter08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        函数高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="chapter08/section01.html">
            
                
                    <a href="../chapter08/section01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        局部变量和全局变量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="chapter08/section02.html">
            
                
                    <a href="../chapter08/section02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        函数返回值详解
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="8.3" data-path="chapter08/section03.html">
            
                
                    <a href="../chapter08/section03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        函数参数详解
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="chapter08/section04.html">
            
                
                    <a href="../chapter08/section04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        可变数据类型和不可变数据类型
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x51FD;&#x6570;&#x53C2;&#x6570;&#x8BE6;&#x89E3;">&#x51FD;&#x6570;&#x53C2;&#x6570;&#x8BE6;&#x89E3;</h2>
<h3 id="&#x4E00;&#x3001;&#x7F3A;&#x7701;&#x53C2;&#x6570;">&#x4E00;&#x3001;&#x7F3A;&#x7701;&#x53C2;&#x6570;</h3>
<p>&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x7F3A;&#x7701;&#x53C2;&#x6570;&#x7684;&#x503C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4F20;&#x5165;&#xFF0C;&#x5219;&#x53D6;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</p>
<p>&#x4E0B;&#x4F8B;&#x4F1A;&#x6253;&#x5370;&#x9ED8;&#x8BA4;&#x7684;age&#xFF0C;&#x5982;&#x679C;age&#x6CA1;&#x6709;&#x88AB;&#x4F20;&#x5165;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">printinfo</span><span class="hljs-params">(name, age=<span class="hljs-number">35</span>)</span>:</span>
   <span class="hljs-comment"># &#x6253;&#x5370;&#x4EFB;&#x4F55;&#x4F20;&#x5165;&#x7684;&#x5B57;&#x7B26;&#x4E32;</span>
   print(<span class="hljs-string">&quot;name: %s&quot;</span> % name)
   print(<span class="hljs-string">&quot;age %d&quot;</span> % age)

<span class="hljs-comment"># &#x8C03;&#x7528;printinfo&#x51FD;&#x6570;</span>
printinfo(name=<span class="hljs-string">&quot;miki&quot;</span>)  <span class="hljs-comment"># &#x5728;&#x51FD;&#x6570;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D; age&#x53D6;&#x9ED8;&#x8BA4;&#x503C;35</span>
printinfo(age=<span class="hljs-number">9</span> ,name=<span class="hljs-string">&quot;miki&quot;</span>)
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">name: miki
age: <span class="hljs-number">35</span>
name: miki
age: <span class="hljs-number">9</span>
</code></pre>
<h3 id="&#x603B;&#x7ED3;&#xFF1A;">&#x603B;&#x7ED3;&#xFF1A;</h3>
<ul>
<li><p>&#x5728;&#x5F62;&#x53C2;&#x4E2D;&#x9ED8;&#x8BA4;&#x6709;&#x503C;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x79F0;&#x4E4B;&#x4E3A;&#x7F3A;&#x7701;&#x53C2;&#x6570;</p>
</li>
<li><p>&#x6CE8;&#x610F;&#xFF1A;&#x5E26;&#x6709;&#x9ED8;&#x8BA4;&#x503C;&#x7684;&#x53C2;&#x6570;&#x4E00;&#x5B9A;&#x8981;&#x4F4D;&#x4E8E;&#x53C2;&#x6570;&#x5217;&#x8868;&#x7684;&#x6700;&#x540E;&#x9762;</p>
<pre><code class="lang-python">  &gt;&gt;&gt; <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">printinfo</span><span class="hljs-params">(name, age=<span class="hljs-number">35</span>, sex)</span>:</span>
  ...     <span class="hljs-keyword">print</span> name
  ...
    File <span class="hljs-string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="hljs-number">1</span>
  SyntaxError: non-default argument follows default argument
</code></pre>
</li>
</ul>
<h3 id="&#x4E8C;&#x3001;&#x4E0D;&#x5B9A;&#x957F;&#x53C2;&#x6570;">&#x4E8C;&#x3001;&#x4E0D;&#x5B9A;&#x957F;&#x53C2;&#x6570;</h3>
<p>&#x6709;&#x65F6;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x80FD;&#x5904;&#x7406;&#x6BD4;&#x5F53;&#x521D;&#x58F0;&#x660E;&#x65F6;&#x66F4;&#x591A;&#x7684;&#x53C2;&#x6570;, &#x8FD9;&#x4E9B;&#x53C2;&#x6570;&#x53EB;&#x505A;&#x4E0D;&#x5B9A;&#x957F;&#x53C2;&#x6570;&#xFF0C;&#x58F0;&#x660E;&#x65F6;&#x4E0D;&#x4F1A;&#x547D;&#x540D;&#x3002;</p>
<p>&#x57FA;&#x672C;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">functionname</span><span class="hljs-params">([formal_args,] *args, **kwargs)</span>:</span>
   <span class="hljs-string">&quot;&quot;&quot;&#x51FD;&#x6570;_&#x6587;&#x6863;&#x5B57;&#x7B26;&#x4E32;&quot;&quot;&quot;</span>
   function_suite
   <span class="hljs-keyword">return</span> [expression]
</code></pre>
<h3 id="&#x6CE8;&#x610F;&#xFF1A;">&#x6CE8;&#x610F;&#xFF1A;</h3>
<ul>
<li>&#x52A0;&#x4E86;&#x661F;&#x53F7;&#xFF08;*&#xFF09;&#x7684;&#x53D8;&#x91CF;args&#x4F1A;&#x5B58;&#x653E;&#x6240;&#x6709;&#x672A;&#x547D;&#x540D;&#x7684;&#x53D8;&#x91CF;&#x53C2;&#x6570;&#xFF0C;args&#x4E3A;&#x5143;&#x7EC4;</li>
<li>&#x800C;&#x52A0;**&#x7684;&#x53D8;&#x91CF;kwargs&#x4F1A;&#x5B58;&#x653E;&#x547D;&#x540D;&#x53C2;&#x6570;&#xFF0C;&#x5373;&#x5F62;&#x5982;key=value&#x7684;&#x53C2;&#x6570;&#xFF0C; kwargs&#x4E3A;&#x5B57;&#x5178;.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span><span class="hljs-params">(a, b, *args, **kwargs)</span>:</span>
      <span class="hljs-string">&quot;&#x51FD;&#x6570;&#x5728;&#x58F0;&#x660E;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&quot;</span>
    print(<span class="hljs-string">&apos;a={},b={},args={},kwargs={}&apos;</span>.format(a,b,args,kwargs))

test(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-string">&apos;&#x4F60;&#x597D;&apos;</span>, <span class="hljs-string">&apos;hi&apos;</span>, <span class="hljs-string">&apos;how do you do&apos;</span>, name=<span class="hljs-string">&quot;zhangsan&quot;</span>, age=<span class="hljs-number">18</span>)
<span class="hljs-comment"># a=2,b=3,args=(&apos;&#x4F60;&#x597D;&apos;, &apos;hi&apos;, &apos;how do you do&apos;),kwargs={&apos;name&apos;: &apos;zhangsan&apos;, &apos;age&apos;: 18}</span>


b = (<span class="hljs-string">&apos;hi&apos;</span>, <span class="hljs-string">&apos;&#x5927;&#x5BB6;&#x597D;&apos;</span>, <span class="hljs-string">&apos;&#x4ECA;&#x5929;&#x5929;&#x6C14;&#x771F;&#x597D;&apos;</span>)
d = {<span class="hljs-string">&apos;name&apos;</span>: <span class="hljs-string">&quot;zhangsan&quot;</span>, <span class="hljs-string">&quot;age&quot;</span>: <span class="hljs-number">19</span>}

<span class="hljs-comment"># &#x6CE8;&#x610F;&#xFF0C;&#x5728;&#x4F20;&#x5165;&#x53C2;&#x6570;&#x65F6;&#x7684;&#x661F;&#x53F7;&#x95EE;&#x9898;&#x3002;</span>
test(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, *b, **d) 
<span class="hljs-comment"># a=10,b=20,args=(&apos;hi&apos;, &apos;&#x5927;&#x5BB6;&#x597D;&apos;, &apos;&#x4ECA;&#x5929;&#x5929;&#x6C14;&#x771F;&#x597D;&apos;),kwargs={&apos;name&apos;: &apos;zhangsan&apos;, &apos;age&apos;: 19}</span>


<span class="hljs-comment"># &#x5982;&#x679C;&#x5728;&#x4F20;&#x503C;&#x65F6;&#xFF0C;&#x4E0D;&#x4F7F;&#x7528;&#x661F;&#x53F7;&#xFF0C;&#x4F1A;&#x628A;&#x540E;&#x9762;&#x7684;&#x53C2;&#x6570;&#x5F53;&#x505A; args</span>
test(<span class="hljs-number">10</span>,<span class="hljs-number">20</span>,b,d)
<span class="hljs-comment"># a=10,b=20,args=((&apos;hi&apos;, &apos;&#x5927;&#x5BB6;&#x597D;&apos;, &apos;&#x4ECA;&#x5929;&#x5929;&#x6C14;&#x771F;&#x597D;&apos;), {&apos;name&apos;: &apos;zhangsan&apos;, &apos;age&apos;: 19}),kwargs={}</span>
</code></pre>
<h3 id="&#x4E09;&#x3001;&#x7F3A;&#x7701;&#x53C2;&#x6570;&#x5728;args&#x540E;&#x9762;">&#x4E09;&#x3001;&#x7F3A;&#x7701;&#x53C2;&#x6570;&#x5728;*args&#x540E;&#x9762;</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sum_nums_3</span><span class="hljs-params">(a, *args, b=<span class="hljs-number">22</span>, c=<span class="hljs-number">33</span>, **kwargs)</span>:</span>
    print(a)
    print(b)
    print(c)
    print(args)
    print(kwargs)

sum_nums_3(<span class="hljs-number">100</span>, <span class="hljs-number">200</span>, <span class="hljs-number">300</span>, <span class="hljs-number">400</span>, <span class="hljs-number">500</span>, <span class="hljs-number">600</span>, <span class="hljs-number">700</span>, b=<span class="hljs-number">1</span>, c=<span class="hljs-number">2</span>, mm=<span class="hljs-number">800</span>, nn=<span class="hljs-number">900</span>)
</code></pre>
<h3 id="&#x8BF4;&#x660E;&#xFF1A;">&#x8BF4;&#x660E;&#xFF1A;</h3>
<ul>
<li>&#x5982;&#x679C;&#x5F88;&#x591A;&#x4E2A;&#x503C;&#x90FD;&#x662F;&#x4E0D;&#x5B9A;&#x957F;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x7F3A;&#x7701;&#x53C2;&#x6570;&#x653E;&#x5230; <em>args&#x7684;&#x540E;&#x9762;&#xFF0C; &#x4F46;&#x5982;&#x679C;&#x6709;<em>*kwargs&#x7684;&#x8BDD;&#xFF0C;</em></em>kwargs&#x5FC5;&#x987B;&#x662F;&#x6700;&#x540E;&#x7684;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter08/section02.html" class="navigation navigation-prev " aria-label="Previous page: 函数返回值详解"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter08/section04.html" class="navigation navigation-next " aria-label="Next page: 可变数据类型和不可变数据类型"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
